package com.zzf.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzf.commonutils.R;
import com.zzf.eduservice.entity.EduTeacher;
import com.zzf.eduservice.entity.vo.TeacherQuery;
import com.zzf.eduservice.service.EduTeacherService;
import com.zzf.servicebase.exceptionhandler.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-03-31
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
//@CrossOrigin//解决跨域访问问题
public class EduTeacherController {

    //访问地址:http://localhost:8001/eduservice/teacher/findAll
    //注入service
    @Autowired
    private EduTeacherService teacherService;
    private long current;
    private long limit;

    //1:查询讲师表中的所有数据
    //rest风格
    @ApiOperation(value = "所有的讲师列表")
    @GetMapping("findAll")
    public R findAllTeacher(){
        //调用service方法实现查询所有的操作
        List<EduTeacher> list = teacherService.list(null);

        return R.ok().data("items",list);
    }

    //2:逻辑删除讲师的方法
    @ApiOperation(value = "逻辑删除讲师")
    @DeleteMapping("{id}")//id值需要通过请求路径传输过来
    public R removeTeacher(@ApiParam(name = "id", value = "讲师ID", required = true)
                           @PathVariable String id) {
        boolean flag = teacherService.removeById(id);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //3:分页查询讲师的方法
    @ApiOperation(value = "分页查询讲师")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(@ApiParam(name = "current",value = "起始数据索引",required = true)
                             @PathVariable long current,
                             @ApiParam(name = "limit",value = "每页条数",required = true)
                             @PathVariable long limit){
        this.current = current;
        this.limit = limit;
        //创建page分页对象
        Page<EduTeacher> teacherPage = new Page<>(current, limit);
        //把分页所有数据封装到pageTeacher对象里面
        teacherService.page(teacherPage, null);

        //数据总条数
        long total = teacherPage.getTotal();
        List<EduTeacher> records = teacherPage.getRecords();

        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("rows",records);

        return R.ok().data(map);
    }

    //4:条件查询带分页
    @ApiOperation(value = "条件查询讲师")
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery){
        //创建page对象
        Page<EduTeacher> teacherPage = new Page<>(current,limit);
        //构建条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        //多条件组合查询
        //mybatis学过动态sql;现在使用mybatis plus来做
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        //判断条件是否为空,如果不为空拼接条件
        if (!StringUtils.isEmpty(name)){
            //构建条件
            wrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        if (!StringUtils.isEmpty(begin)){
            //大于等于开始时间
            wrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end)){
            //小于等于结束时间
            wrapper.le("gmt_create",end);
        }

        //排序
        wrapper.orderByDesc("gmt_create");

        //把分页所有数据封装到pageTeacher对象里面
        teacherService.page(teacherPage, wrapper);

        //数据总条数
        long total = teacherPage.getTotal();
        List<EduTeacher> records = teacherPage.getRecords();

        HashMap<String, Object> map = new HashMap<>();
        map.put("total",total);
        map.put("rows",records);

        return R.ok().data(map);
    }

    //添加讲师的接口方法
    @ApiOperation(value = "添加讲师")
    @PostMapping("addTeacher")
    public R addTeacher(@ApiParam(name = "eduTeacher",value = "讲师对象",required = true)
                        @RequestBody EduTeacher eduTeacher){
        boolean save = teacherService.save(eduTeacher);
        if (save){
            return R.ok();
        }else {
            return R.error();
        }
    }

    //根据讲师id查询讲师
    @ApiOperation(value = "根据讲师id查询讲师")
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable String id){
        EduTeacher userById = teacherService.getById(id);
        return R.ok().data("teacher",userById);
    }

    //讲师修改功能
    @ApiOperation(value = "讲师修改")
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean b = teacherService.updateById(eduTeacher);
        if (b){
            return R.ok();
        }else {
            return R.error();
        }
    }
}



